package common;
/**
 * @author Idan Mann, Adam Daxa
 * This class holds service methods used by other classes.
 */
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class CommonMethods 
{

	/**
	 * 
	 * This method takes a string and removes all " ' ", " " " and " ; " characters from the string
	 * making them suitable for use in the database.
	 * @param targetString
	 * @return a reformatted string suitable for the database
	 */
	public static  String cleanStringForQuery(String targetString)
	{
	
		String test = (targetString.replaceAll("'", "").replaceAll(";", ".").replaceAll(Character.toString('"'), ""));
		
		return test;		
	}
	
	/**
	 * 
	 * This methods creates a new string with today's date.
	 * @return today's date as a string
	 */
	public static String getDate()
	{
		SimpleDateFormat DateFormat = new SimpleDateFormat("dd/MM/yyyy");
		DateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
		return DateFormat.format(new Date());
	}
	
	public static String getDateTime()
	{
		SimpleDateFormat DateFormat = new SimpleDateFormat("dd/MM/yyyy-HH:mm:ss");
		DateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
		return DateFormat.format(new Date());
	}
	
	/**
	 * 
	 * this method is called after we know about an exceeded request from any type so it calculates the number of days exceeded. 
	 * @param b=deadline
	 * @return absolute value of days exceeded .
	 */
	
	public static int getExceededTime(String b){
	  	String myDate=b;
	  	int diffInDays;
	  	Calendar myCal = Calendar.getInstance();		
		java.util.Date newerDate = new java.util.Date();
		char [] charArray = new char[10];
		charArray=myDate.toCharArray();
		String day = (""+charArray[0]+charArray[1]);
		String mounth = (""+charArray[3]+charArray[4]);
		String year = (""+charArray[6]+charArray[7]+charArray[8]+charArray[9]);
	
		int year1=Integer.parseInt(year);
		int mounth1=Integer.parseInt(mounth);
		int day1 = Integer.parseInt(day);
		myCal.set(year1,mounth1-1,day1);
		java.util.Date myDate1 = myCal.getTime();
		
		diffInDays = (int)( (newerDate.getTime() - myDate1.getTime()) 
				                 / (1000 * 60 * 60 * 24) );
	
		return diffInDays;
	}
	
	/**
	 * 
	 * This method test a given deadline if it's exceeding today's date
	 * @param deadlineDate a String representing the deadline date
	 * @return true if the deadline is exceeded false otherwise;
	 */
	public static boolean exceededDeadline (String deadlineDate)
	{
		boolean answer;
		String today = getDate();
		
		int currentDate;
		int deadline;
		int currentYear;
		int currentMonth;
		int currentDay;
		int deadlineYear;
		int deadlineMonth;
		int deadlineDay;
		
		currentDate = Integer.parseInt(today.replaceAll("/",""));
		deadline = Integer.parseInt(deadlineDate.replaceAll("/", ""));
		
		currentDay = currentDate/1000000;
		currentMonth = (currentDate/10000)%100;
		currentYear = currentDate%10000;
		
		deadlineDay = deadline/1000000;
		deadlineMonth = (deadline/10000)%100;
		deadlineYear = deadline%10000;
		
		if (	(deadlineYear - currentYear)<0	)
			return true;
		else
			if (	(deadlineMonth - currentMonth)<0 && (deadlineYear == currentYear)	)
				return true;
			else
				if ( (deadlineDay - currentDay) < 0 && (deadlineMonth == currentMonth) && (deadlineYear==currentYear))
					return true;
				else
					return false;
	}
	
	/**
	 * 
	 * This string tests if the deadline string is more than 3 days away.
	 * @param deadlineString - a string representing the deadline.
	 * @return true if there are more than 3 days to the date, false otherwise.
	 */
	public static boolean moreThan3Days (String deadlineString)
	{		
		String today = CommonMethods.getDate();
		String tomorrow;
		String theDayAfterTommorow;
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		Calendar cal = Calendar.getInstance();
		try 
		{
			cal.setTime(dateFormat.parse(today));
		} catch (ParseException e)
		{
		//	e.printStackTrace();
		}
		
		cal.add(Calendar.DATE, 1);  // add one day
		tomorrow = dateFormat.format(cal.getTime());  // set date for tomorrow
		cal.add(Calendar.DATE, 1);  // add one day
		theDayAfterTommorow = dateFormat.format(cal.getTime());  // set date for the day after tomorrow
		
		// if the deadline is equal to any of these three it is not over 3 days to the deadline
		if (today.equalsIgnoreCase(deadlineString) || tomorrow.equalsIgnoreCase(deadlineString) || theDayAfterTommorow.equalsIgnoreCase(deadlineString))
			return false;
		
		return true;
	}

	/**
	 * 
	 * This method adds a given amount of days to a date and returns the new date.
	 * @param date - String representing the date in dd/MM/yyyy format.
	 * @param days - number of days to add.
	 * @return a String with the new date
	 */
	public static String addDurationToDate (String date, int days)
	{
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		
		try 
		{
			cal.setTime(dateFormat.parse(date));
		} catch (ParseException e)
		{
			//e.printStackTrace();
		}
		
		cal.add(Calendar.DATE, days);  // add days to the date
		
		return dateFormat.format(cal.getTime());  // return new date string
	}
	
	/**
	 * 
	 * This method adds a given number of days to todays date.
	 * @param numberOfDays - number of days to add to the date.
	 * @return a "dd/MM/yyyy" formatted string with the new date. 
	 */
	public static String createNewDeadline(int numberOfDays)
	{
		String today = getDate();
		String newdeadline = addDurationToDate(today, numberOfDays);
		
		return newdeadline;
		
	}
	
	/**
	 * This method calculates the number of days between two dates
	 * @param date1 date2 teh two dates
	 * @return an integer for the number of days
	 */
	public static int numberOfDays(String date1, String date2)
	{
		int i=0;
		while (!date1.equalsIgnoreCase(date2))
		{
			i++;
			date1 = addDurationToDate(date1, 1);
		}
		return i;
	}
	
}

